﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace WpfPdfReader
{
    public static class WindowFullScreenHelper
    {
        static bool isFullScreen = false;
        static WindowState preWinState;
        static Rect preRestoreBounds;
        static WindowStyle preWinStyle;
        static Window ownerWindow;
        static Key theShortKey;

        /// <summary>
        /// 将theWindow注册全屏操作
        /// </summary>
        /// <param name="theWindow">需要全屏操作的窗体</param>
        /// <param name="shortKey">注册的全屏快捷键</param>
        public static void RegisterFullScreen(this Window theWindow, Key shortKey)
        {
            ownerWindow = theWindow;
            theShortKey = shortKey;
            theWindow.PreviewKeyDown += new System.Windows.Input.KeyEventHandler(theWindow_PreviewKeyDown);
        }

        static void theWindow_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == theShortKey)
            {
                if (!isFullScreen)
                {
                    preWinState = ownerWindow.WindowState;
                    preRestoreBounds = ownerWindow.RestoreBounds;
                    preWinStyle = ownerWindow.WindowStyle;

                    ownerWindow.WindowState = System.Windows.WindowState.Normal;
                    ownerWindow.WindowStyle = System.Windows.WindowStyle.None;
                    ownerWindow.ResizeMode = System.Windows.ResizeMode.NoResize;
                    //ownerWindow.Topmost = true;

                    ownerWindow.Left = 0.0;
                    ownerWindow.Top = 0.0;
                    ownerWindow.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                    ownerWindow.Height = System.Windows.SystemParameters.PrimaryScreenHeight;

                    isFullScreen = true;
                }
                else
                {
                    ownerWindow.WindowState = preWinState;
                    ownerWindow.WindowStyle = preWinStyle;
                    ownerWindow.ResizeMode = System.Windows.ResizeMode.CanResize;
                    ownerWindow.Topmost = false;

                    ownerWindow.Left = preRestoreBounds.Left;
                    ownerWindow.Top = preRestoreBounds.Top;

                    ownerWindow.Width = preRestoreBounds.Width;
                    ownerWindow.Height = preRestoreBounds.Height;

                    isFullScreen = false;
                }
            }
        }
    }
}
